package entity.lemming;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import entity.terrain.Terrain;

/**
 * A Lemming of infinite energy that continuously makes progress in the x-plane.
 * 
 * @author Neil Cowzer
 */
public class Walker extends Lemming {
	/**
	 * Create a new Walker.
	 * 
	 * @param level The level which the Walker exist in.
	 * @param x The x-coordinate of the Walker.
	 * @param y The y-coordinate of the Walker.
	 */
	/*@ requires 0 <= x && 0 <= y;
		assignable speed_X && speed_Y && energy;
		ensures speed_X != 0 && energy == 100;
	 */
	public Walker(int x,int y) {
		// Hardcode Image location
		super("resource/graphics/lemming/walker/walker1.gif",9, x, y);
		speed_X = 1;
		speed_Y = 0;
		energy = 100;
		collide = false;
	}
	
	/**
	 * Create a new Walker with the given center point
	 * 
	 * @param center point of the Lemming
	 */
	public Walker(Point center){
		super("resource/graphics/lemming/walker/walker1.gif",9, center);
		speed_X = 1;
		speed_Y = 0;
		energy = 100;
		collide = false;
	}
	
	/** Request that this lemming perform the appropiate action/movement. */
	/*@ ensures x != \old(x) + speed_X &&
				energy == 100;
	 */
	public void move(Terrain terrain){
		// calculate the next y coordinate
		int new_y = y;
		Rectangle lemming = getBounds();
		
		if (lemming.intersects(terrain.getBounds())){
			BufferedImage image = ((Terrain)terrain).getImage().getBufferedImage();
				int black = 16777215; // black that represents the background
				
				try{
					
					for (int curr_y = lemming.y; curr_y < lemming.y + lemming.height + (lemming.height/2); curr_y++){
						int color = 0;
						
						
							if (speed_X > 0){
								color = image.getRGB(x + lemming.width, curr_y);
							}
							else {
								color = image.getRGB(x + 3, curr_y);
							}
						
							// if we have a pixel to stand on
							if (color != black){
								if(curr_y <= lemming.y + lemming.height/2){
										turnAround();
								}
								else{
									new_y = curr_y - lemming.height -1;
								}
								break;
							}
							else{
								// Gaurd! not allowed fall unless all the "ground" beneath him is black
								boolean onlyBlack = true;
								while (onlyBlack){
									for (int fx = lemming.x; fx<lemming.x+lemming.width; fx++){
										for (int fy = lemming.y+lemming.height; fy <lemming.y+lemming.height+lemming.height/2; fy++){
											int testcolor =  image.getRGB(fx, fy);
											//System.out.println(testcolor);
											if (testcolor != black){
												onlyBlack = false;
											}
										}
									}
									break;
								}
								if (onlyBlack){
									fall = true;
								}
								
							}
					}
				}catch(ArrayIndexOutOfBoundsException e){
					
				}
		}
		
		// always perform a move in the x direction
		x= x+speed_X;
		y = new_y;	
		update();
		//System.out.println("x: "+ x+ ", y: "+y);
	}
	
	/** Reverse a Lemmings direction */
	//@ ensures speed_X == 0 - \old(speed_X);
	public void turnAround(){
		speed_X = -(speed_X);
		
		// change the images accordingly
		if (speed_X > 0){
			generateFrames("resource/graphics/lemming/walker/walker1.gif", 8);
		}
		else {
			generateFrames("resource/graphics/lemming/walker/walkerLeft_1.gif", 8);
		}
	}
	
	/**
	 * Check to see if a Lemming collides with another entity
	 * 
	 * @param other another lemming.
	 * @return true if collision occurs.
	 */
	public /*@ pure @*/ boolean collision(Lemming other){
		// get my size
		Rectangle me = getBounds();
		// if we hit a blocker
		if (other instanceof Blocker){
			if (me.intersects(other.getBounds())){
				//turnAround();
				return true;
			}
		}
				return false;
	}
}
